package io.renren.modules.dictionary.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.CustomUtils;
import io.renren.common.utils.R;
import io.renren.modules.dictionary.entity.DictionaryEntity;
import io.renren.modules.dictionary.service.DictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Title: stf
 * @Description: 积分管理controller
 * @Author: 杨帅
 * @CreateDate: 2018/11/26 14:33
 * @Version: 1.0
 */
@RestController
@RequestMapping("/pointmanagement/pointmanagement")
public class IntegralController {
    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 修改
     */
    @SysLog("修改发布、初始、浏览积分")
    @PostMapping("/updateIntegral")
    @RequiresPermissions("pointmanagement:pointmanagement:update")
    public R updateIntegral(@RequestBody DictionaryEntity dictionary){
       if (dictionary.getDictionaryvalue() != null && !"".equals(dictionary.getDictionaryvalue())){
           boolean isInteger = CustomUtils.isInteger(dictionary.getDictionaryvalue());
           if (isInteger){
               dictionary.setUpdatetime(new Date());
               dictionaryService.updateByDataTypeName(dictionary);
               return R.ok();
           }else {
               return R.error("积分应该为数字且不能为负数");
           }
       }
        return R.error("积分不能为空");
    }


    /**
     * 修改
     */
    @SysLog("修改优惠")
    @PostMapping("/updatediscounts")
    @RequiresPermissions("pointmanagement:pointmanagement:update")
    public R updateDiscounts(@RequestBody DictionaryEntity dictionary){
        if (dictionary.getDictionaryvalue() != null && !"".equals(dictionary.getDictionaryvalue())){
            boolean isInteger = CustomUtils.isInteger(dictionary.getDictionaryvalue());
            if (isInteger){
                int value = Integer.parseInt(dictionary.getDictionaryvalue());
                if (value >=0 && value <= 10){
                    dictionary.setUpdatetime(new Date());
                    dictionaryService.updateByDataTypeName(dictionary);
                    return R.ok();
                }
                return R.error("优惠范围应该在(0-10)之间");
            }else {
                return R.error("优惠应该为数字且不能为负数");
            }
        }
        return R.error("优惠不能为空");
    }
}
